import socket from './index'
import $bus from '@/utils/eventBus'
import { useUserInfoStore } from '../store/modules/user'
import { useContactStore } from '@/store/modules/contact'
import { IMessageType } from '@/store/modules/interface/message'
import utils from '@/utils'
import router from '@/router'
import { useMessageStore } from '@/store/modules/message'

// 当前路由
const route = router.currentRoute
const userInfoStore = useUserInfoStore()
const contactStore = useContactStore()
const messageStore = useMessageStore()

socket.on('login', () => {
	// 初始化申请消息通知
	socket.emit('init_apply_notice', userInfoStore.userInfo._id)
	console.log('用户socket链接成功')
})

// 初始化申请消息通知
socket.on('init_apply_notice', (val) => {
	contactStore.SOCKET_ContactNum(val)
})

// 侦听申请消息通知
socket.on('apply_notice', (val) => {
	console.log(',,,', val)
	contactStore.SOCKET_ContactNum(val)
})

// 同意好友申请
socket.on('agree_apply', (id) => {
	$bus.emit('apply')
	if (!id) return
	// 同意加入群聊
	console.log('同意加入群聊', id)
	socket.emit('agree_apply_join_group', id)
})

// 监听私聊消息
socket.on('private_chat', async (val: IMessageType) => {
	console.log('66666', val)
	val.time = utils.formatTime(new Date(val.time))
	// 好友id
	const urlId = route.value.query.id
	// 左侧列表用户的索引
	const currentIndex = messageStore.messageList.findIndex((item) => item._id === val.userId._id)
	console.log('当前索引', currentIndex)
	// 还未与该好友聊过天，没有存在过聊天列表
	if (currentIndex === -1) {
		const data = {
			imgUrl: val.userId.imgUrl,
			lastMsg: {
				time: val.time
			},
			nick: val.userId.nick,
			type: 'friendChat',
			// 未读消息数量
			unreadMsgCount: 0,
			_id: val.fromId as string
		}
		messageStore.messageList.unshift(data)
	} else {
		// 存在过聊天记录，把他置顶
		messageStore.messageList.unshift(messageStore.messageList.splice(currentIndex, 1)[0])
	}
	messageStore.messageList[0].lastMsg!.time = new Date()
	// 接收者客户端的 urlId和发送者id相同
	if (urlId === val.fromId) {
		console.log(val)
		messageStore.messageChatList.push(val)
		console.log(messageStore.messageChatList)
	}
	messageStore.messageList.forEach((item) => {
		if (item._id === urlId) {
			item.userStatus = true
		}
	})
})

// 监听未读消息 val:群聊id
socket.on('unread_message', (val: string) => {
	console.log(val, '群聊id')
	console.log(route.value.query.id)
	if (route.value.query.id === val) {
		// 已经和这个人或者群里聊天中，不需要通知
		messageStore.triggerMessageRead()
		return
	}
	messageStore.messageTotal++
	messageStore.messageList.forEach((item) => {
		if (item._id === val) {
			item.unreadMsgCount++
		}
	})
})

// 监听群聊消息
socket.on('group_chat', (val: IMessageType) => {
	console.log('vvv', val)
	val.time = utils.formatTime(new Date(val.time))
	const urlId = route.value.query.id
	// 左侧列表用户的索引
	const currentIndex = messageStore.messageList.findIndex((item) => val.toId === item._id)
	if (currentIndex == -1) {
		const data = {
			imgUrl: val.groupInfo!.imgUrl as string,
			lastMsg: {
				time: val.time
			},
			groupName: val.groupInfo?.groupName,
			type: 'groupChat',
			unreadMsgCount: 0,
			_id: val.groupInfo!._id as string
		}
		console.log('置顶消息', data)
		messageStore.messageList.unshift(data)
	} else {
		messageStore.messageList.forEach((item, index) => {
			if (index === currentIndex) messageStore.messageList.unshift(messageStore.messageList.splice(index, 1)[0])
		})
	}
	// 改变消息列表中, 最近一次消息的时间
	messageStore.messageList[0].lastMsg!.time = new Date()
	if (urlId === val.toId) {
		messageStore.messageChatList.push(val)
	}
	messageStore.messageList.forEach((item) => {
		if (item._id === urlId) {
			item.userStatus = true
		}
	})
})

// 重新连接群聊
socket.on('again_concat_group', () => {
	messageStore.getAllGroup()
})

// 测试群聊消息
socket.on('testGost', () => {
	console.log('测试群消息成功')
})

socket.on('disconnect', (res) => {
	console.log(res)
	console.log('断开')
})
